import { defineStore } from "pinia";
import { reqGetCategory3List, type categoryListType } from "@/api/category";
import { reqGetCategory1List, reqGetCategory2List } from "@/api/category";

// categoryStore  state中的数据类型
export interface categoryStoreItemType {
  category1List: categoryListType;
  category2List: categoryListType;
  category3List: categoryListType;
  category1Id: number | null;
  category2Id: number | null;
  category3Id: number | null;
}

const useCategoryStore = defineStore("category", {
  state: (): categoryStoreItemType => {
    return {
      category1List: [],
      category2List: [],
      category3List: [],
      category1Id: null,
      category2Id: null,
      category3Id: null,
    };
  },

  actions: {
    //   请求一级列表数据及保存
    async getCategory1List() {
      const re = await reqGetCategory1List();
      this.category1List = re;
    },
    //   请求二级列表数据及保存
    async getcategory2List() {
      const re = await reqGetCategory2List(this.category1Id as number);
      this.category2List = re;
    },
    //   请求三级列表数据及保存
    async getcategory3List() {
      const re = await reqGetCategory3List(this.category2Id as number);
      this.category3List = re;
    },
  },
});

export default useCategoryStore;
